The core logic for circular pointer updates in enQueue and deQueue operations.
- Having established the distinct conditions for an empty and a full queue, we can now implement the core operations. Both enQueue and deQueue require a boundary check and then the circular update of their pointers.
- The central component driving circularity and ensuring $O(1)$ performance is the modulo operator (%).
- Key Operation: Pointer Increment & Wrap
$\text{pointer} = (\text{pointer} + 1) \ \% \ MAX\_SIZE$
enQueue(Insertion):- Check: Verify the queue is not full.
- Update
rear: Calculate the newrearindex using the modulo operation. This handles wrapping (e.g., from index 4 to 0 when $MAX\_SIZE = 5$). - Assign: Insert the new element at the new
rearindex.
deQueue(Removal):- Check: Verify the queue is not empty.
- Retrieve: Get the element at the current
frontindex. - Update
front: Calculate the newfrontindex using the modulo operation.
The Modulo Operator
The modulo operator (%) is the cornerstone of the circular array implementation. It elegantly handles the "wrap-around" logic by finding the remainder of a division, ensuring that pointer indices always stay within the valid range of $0$ to $MAX\_SIZE - 1$.